package com.goldenautumn.prefabricate.activity.home

import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ActivityUtils
import com.goldenautumn.prefabricate.MainActivity
import com.goldenautumn.prefabricate.business.entity.HomeItemDataVo
import com.youth.banner.adapter.BannerAdapter

open class HomeMenuBannerAdapter(datas: List<List<HomeItemDataVo>>) : BannerAdapter<List<HomeItemDataVo>, HomeMenuBannerAdapter.HomeMenuViewHolder>(datas) {

    override fun onCreateHolder(parent: ViewGroup, viewType: Int): HomeMenuViewHolder {
        val recyclerView = RecyclerView(parent.context)
        var params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
//        params.setMargins(
//            SizeUtils.dp2px(10f),
//            SizeUtils.dp2px(17f),
//            SizeUtils.dp2px(10f),
//            SizeUtils.dp2px(24f),
//        )
        recyclerView.layoutParams = params
        return HomeMenuViewHolder(recyclerView)
    }

    override fun onBindView(holder: HomeMenuViewHolder?, data: List<HomeItemDataVo>?, position: Int, size: Int) {
        data?.let {
            var mAdapter = HomeMenuAdapter()
            holder?.recyclerView?.layoutManager = GridLayoutManager(holder?.recyclerView?.context, 4)
            holder?.recyclerView?.adapter = mAdapter
            mAdapter.submitList(it)
            mAdapter.setOnItemClickListener { adapter, view, position ->
                val item = adapter.getItem(position)
                var activity = ActivityUtils.getTopActivity()
                if(activity is MainActivity) activity.goHome(1,item?.categoryId)
            }
        }
    }

    class HomeMenuViewHolder(@NonNull view: RecyclerView) : RecyclerView.ViewHolder(view) {
        var recyclerView: RecyclerView = view
    }

}